package com.freecloud.whispered.auth.service.impl;

import com.freecloud.framework.service.AbstractBaseService;
import com.freecloud.whispered.auth.entity.SysUserRoleEntity;
import com.freecloud.whispered.auth.repository.SysUserRoleDao;
import com.freecloud.whispered.auth.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 用户与角色对应关系
 * 
 * @author maomao
 * @email 138383877@qq.com
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl extends AbstractBaseService<SysUserRoleEntity,Long, SysUserRoleDao> implements SysUserRoleService {
	@Autowired
	private SysUserRoleDao sysUserRoleDao;

	@Override
	public void saveOrUpdate(Long userId, List<Long> roleIdList) {
		if(roleIdList.size() == 0){
			return ;
		}
		
		//先删除用户与角色关系
		sysUserRoleDao.deleteByUserId(userId);

		List<SysUserRoleEntity> list = new ArrayList<>();

		roleIdList.stream().forEach(e ->{
			SysUserRoleEntity obj = new SysUserRoleEntity();
			obj.setUserId(userId);
			obj.setRoleId(e);

			list.add(obj);
		});
		sysUserRoleDao.saveAll(list);
	}

	@Override
	public List<Long> queryRoleIdList(Long userId) {
		return sysUserRoleDao.queryRoleIdList(userId);
	}
}
